local WU = require "packages/wdtx/WUltiity"

local MobileSelGenMuty = class("MobileSelGenMuty")
local HobbyGenPool = require "packages/wdtx/HobbyGenPool"
MobileSelGenMuty.COMMAND = "MiniGame"
MobileSelGenMuty.GAMETYPE = "ChooseGeneralWithCanChangeForWDTX"
MobileSelGenMuty.POOLTYPE = {
  Players ="Players",
  AIs = "AIs"
}
function MobileSelGenMuty:initialize(data)
  self.room = Fk:currentRoom()
  self.generalNum = data.generalNum
  -- self.repcount = data.repcount
  self.istest = data.istest
  -- self.openhobbygens = data.openhobbygens
  self.genpoolname = data.genpoolname
  self.aipoolname = data.aipoolname or data.genpoolname
  self.jumpsysaipool = data.jumpsysaipool or false
  if not data.closeskin then
    self:openIsSeeSkin()
  end
  -- if data.openhobbygens then
  --   self:selGensByHobby()
  -- end
  self:openMutySelGens()
end
--打开多人选将面板
function MobileSelGenMuty:openMutySelGens()
  local room = self.room
  local command,game_type = MobileSelGenMuty.COMMAND,MobileSelGenMuty.GAMETYPE
  local game = Fk.mini_games[game_type]
  if not game then return end

  local players = room.players
  local pushPlayerGensHandle = function(genspool,num)
    local n = num
    local tmp = genspool
    local gens = {}
    while n > 0 and #tmp > 0 do
      local i = math.random(1, #tmp)
      table.insert(gens, table.remove(tmp, i))
      n = n - 1
    end
    return gens
  end

  -- local aigenpool = HobbyGenPool:getgenPool(self.aipoolname,MobileSelGenMuty.POOLTYPE.AIs)
  -- local aigens = table.random(aigenpool,30)
  local aigens
  if not self.jumpsysaipool then
    if #Fk.MobileAIGens > 10 then
      aigens = pushPlayerGensHandle(Fk.MobileAIGens,8 + #table.filter(room.players,function(pr) return pr.role == "loyalist" and pr.id < 0 end))
    else
      Fk.MobileAIGens = table.clone(HobbyGenPool:getgenPool(self.aipoolname,MobileSelGenMuty.POOLTYPE.AIs))
      aigens = pushPlayerGensHandle(Fk.MobileAIGens,8 + #table.filter(room.players,function(pr) return pr.role == "loyalist" and pr.id < 0 end))
    end
  else
    aigens = pushPlayerGensHandle(table.clone(HobbyGenPool:getgenPool(self.aipoolname,MobileSelGenMuty.POOLTYPE.AIs)),8)
  end
  local generals
  -- local generals = table.random(HobbyGenPool:getgenPool(self.genpoolname,MobileSelGenMuty.POOLTYPE.Players),self.generalNum)
  if #Fk.MobilePlayerGens > 10 then
    generals = pushPlayerGensHandle(Fk.MobilePlayerGens,self.generalNum)
  else
    Fk.MobilePlayerGens = table.clone(HobbyGenPool:getgenPool(self.genpoolname,MobileSelGenMuty.POOLTYPE.Players))
    generals = pushPlayerGensHandle(Fk.MobilePlayerGens,self.generalNum)
  end
  local all_cards = pushPlayerGensHandle(Fk.MobilePlayerGens, self.generalNum)
  local realplayernum = 0
  local selgen_cards = {}
  for _,pr in ipairs(players) do
    if pr.id > 0 then realplayernum = realplayernum + 1 end
    if pr.role == "loyalist" or pr.role == "lord" then
      table.insert(selgen_cards,"请选将")
    end
    if self.istest and pr.id > 0 then
      table.insert(generals,self.istest)
    end
  end
  local seatmap = {}
  local realplayers = {}
  local wddata = {}
  for i=1,#selgen_cards do
    seatmap[i] = 2*i-1
  end
  for _,pr in ipairs(players) do
    if pr.id > 0 then
      local repgens = {}
      -- local data = {seat = pr.seat,gens = generals,seats = seatmap,realnum = realplayernum,selected_cards = selgen_cards}
      local n = room:getSettings("enableDeputy") and 2 or 1
      local data = {
        change = all_cards,
        cards = generals,  --generals
        num = n,  --n
        no_c = false, --no_convert
        heg = false, --heg
        type = "askForGeneralsChosen", --rule
        extra = {n = n}, --extra_data
      }
      pr.mini_game_data = { type = game_type, data = data}
      table.insert(realplayers,pr)
    else
      pr.default_reply = table.remove(aigens,1) --忠臣反贼人机将池
    end
  end

  room.aiselgens = aigens
  for _,pr in ipairs(realplayers) do
    if pr.id > 0 and pr.mini_game_data then
      pr.request_data = pr.mini_game_data
      pr.default_reply = game.default_choice and game.default_choice(pr, pr.mini_game_data.data) or ""
      wddata[pr.id] = pr.mini_game_data.data
    end
  end

  -- skill_name = data.skillname;
  -- options = data.choices ?? [];
  -- min_num = data.minnum;
  -- max_num = data.maxnum;
  local req = room:askToMiniGame(realplayers,{
    skill_name = "AskForGeneral",
    game_type = game_type,
    data_table = wddata,
    timeout = room:getSettings("generalTimeout"),
  })

  for _, p in ipairs(players) do
    p.mini_game_data = nil
    if req:getResult(p) == "" then
      req.result[p.id] = table.remove(aigens,1)
    end
  end

  for _, pr in ipairs(players) do
    -- p(pr.general)
    -- p(pr.client_reply)
    -- dbg()
    if pr.general == "" and req:getResult(pr) then
      local general = req:getResult(pr)
      room:setPlayerGeneral(pr, general, true)
    else
      room:setPlayerGeneral(pr, "blank_shibing",true)
    end
    pr.default_reply = ""
  end

  for _, p in ipairs(realplayers) do
    local result = req:getResult(p) or {"guanyu", "liubei"}
    room:prepareGeneral(p, result[1], result[2], true)
  end

  -- room:askToChooseKingdom(players)
  for _, p in ipairs(players) do
    room:broadcastProperty(p, "general")
    room:broadcastProperty(p, "kingdom")
  end
end

--打开观看皮肤
function MobileSelGenMuty:openIsSeeSkin()
  local room = self.room
  self:openMutyDetailedCheck("#skinhobby",1,1,{{type=1,name="seeskin"},{type=1,name="noskin"}})
  for i, pr in ipairs(room.players) do
    if pr.id > 0 then
      local selchs = pr.client_reply
      if selchs == "seeskin" then pr.seeskin = true end
    end
  end
end
-- --打开共享多选选项界面
function MobileSelGenMuty:openMutyDetailedCheck(name,min,max,chc)
  local room = self.room
  local command,game_type = MobileSelGenMuty.COMMAND,"mutyselchc"
  local game = Fk.mini_games[game_type]
  if not game then return end
  local realplayers = {}
  for _, pr in ipairs(room.players) do
    if pr.id > 0 then
      local data = {skillname=name,minnum=min,maxnum=max,choices=chc} 
      pr.mini_game_data = { type = game_type, data = data}
      pr.request_data = pr.mini_game_data
      pr.default_reply = game.default_choice and game.default_choice(pr, pr.mini_game_data.data) or ""
      table.insert(realplayers,pr)
    end 
  end
  room:notifyMoveFocus(realplayers, "AskForChoice")
  WU:doBroadcastRequest(room, command, realplayers)
  for _, pr in ipairs(realplayers) do
    pr.mini_game_data = nil
    if not pr.reply_ready then
      pr.client_reply = pr.default_reply
    end
    local dedata = pr.client_reply
    if dedata and #dedata == 1 then
      pr.client_reply = dedata[1].name
    end
    pr.default_reply = ""
  end
end

-- --根据爱好筛选将池
-- function MobileSelGenMuty:selGensByHobby()
--   local room = self.room
--   local hobbygens = {gender ={{},{}}}
--   local totalnum = self.generalNum + self.repcount
  
--   local playerGens = HobbyGenPool:getgenPool(self.genpoolname,MobileSelGenMuty.POOLTYPE.Players)
--   for _,gen in ipairs(playerGens) do
--     local gender = Fk.generals[gen].gender
--     local gendergens = hobbygens.gender[gender]
--     if gendergens then
--       table.insert(hobbygens.gender[gender],gen)
--     end
--   end

--   self:openMutyDetailedCheck("#selhobby",1,1,{{type=1,name="randsel",price=20},{type=1,name="allmale",price=30},{type=1,name="allfemale",price=40}})

--   table.shuffle(hobbygens.gender[1])
--   table.shuffle(hobbygens.gender[2])
--   local splitgens = function(pr,filtergens,num)
--     pr.selgens = {}
--     if not filtergens or #filtergens == 0 then
--       for i=1,num do
--         local swithgender = i % 2
--         if swithgender == 0 then
--           swithgender = 2
--         end
--         local switchgens = hobbygens.gender[swithgender]
--         if #switchgens > 0 then         
--           table.insert(pr.selgens,table.remove(switchgens,1))
--         end
--       end
--     else
--       for i=1,num do
--         table.insert(pr.selgens,table.remove(filtergens,1))
--       end
--     end
--   end
--   local hobbychoices = {allmale = 1,allfemale = 2}
--   for i, pr in ipairs(room.players) do
--     if pr.id > 0 then
--       local selchs = pr.client_reply
--       splitgens(pr, hobbygens.gender[hobbychoices[selchs]],totalnum)
--     end
--   end
-- end

return MobileSelGenMuty